package com.example.reader.sdk;

import android.util.Log;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;

public class RootCommand {
    private static final String HEAD_EXEC = "EXEC_CMD|";
    private static final String HEAD_MOVE_FILE = "MOVE_FILE|";
    /* renamed from: IP */
    private static final String f24IP = "127.0.0.1";
    private static final String NO_ERROR = "0";
    private static final int PORT = 4757;
    private static final String PWD = "SHINE_USER";
    private static final String TAG = "RootCommand";
    private DataInputStream mDataInputStream;
    private DataOutputStream mDataOutputStream;

    private boolean connect() {
        try {
            Socket socket = new Socket(f24IP, PORT);
            this.mDataOutputStream = new DataOutputStream(socket.getOutputStream());
            this.mDataInputStream = new DataInputStream(socket.getInputStream());
            this.mDataOutputStream.writeUTF(PWD);
            return NO_ERROR.equals(this.mDataInputStream.readUTF());
        } catch (IOException e) {
            Log.e(TAG, e.toString());
            return false;
        }
    }

    public boolean executeCommand(String command) {
        if (connect()) {
            boolean equals = false;
            try {
                this.mDataOutputStream.writeUTF(HEAD_EXEC + command);
                equals = NO_ERROR.equals(this.mDataInputStream.readUTF());
            } catch (IOException e) {
                Log.e(TAG, "get IOException while execute " + command);
            } finally {
                close(this.mDataInputStream, this.mDataOutputStream);
            }
            return equals;
        }
        close(this.mDataInputStream, this.mDataOutputStream);
        return false;
    }

    public void executeCommands(String... commands) {
        if (connect()) {
            for (String command : commands) {
                try {
                    this.mDataOutputStream.writeUTF(HEAD_EXEC + command);
                    Log.d(TAG, command + " 执行结果 " + NO_ERROR.equals(this.mDataInputStream.readUTF()));
                } catch (IOException e) {
                    Log.d(TAG, command + " 执行中IOException");
                }
            }
        }
        close(this.mDataInputStream, this.mDataOutputStream);
    }

    public void executeCommands(List<String> commands) {
        if (connect()) {
            for (String command : commands) {
                try {
                    this.mDataOutputStream.writeUTF(HEAD_EXEC + command);
                    Log.d(TAG, command + " 执行结果 " + NO_ERROR.equals(this.mDataInputStream.readUTF()));
                } catch (IOException e) {
                    Log.d(TAG, command + " 执行中IOException");
                }
            }
        }
        close(this.mDataInputStream, this.mDataOutputStream);
    }

    private void close(InputStream inputStream, OutputStream outputStream) {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
    }

}
